﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;

namespace MyWebToolingCollection
{

    public partial class FormMain : Form
    {
        /*定义变量值:
         * 0 表示不包含头，且不包含尾
         * 2 表示包含头，但不包含尾
         * 3 表示包含头，且包含尾
         * 1 表示不包含头，但包含尾
         * 记忆逻辑参考二进数表示，如3二进制表示为11
         */
        public static int SUBSTRING_HEAR_INCLUDE = 0;

        /*
         * 默认是0未开启，每个值对应一个CheckedBox；
         * 详见whichMethodNowForRegexFiles()函数定义！
         * */
        private static int MYMETHOD_SUBSTRING_USE = 0;
        private static int MYMETHOD_SUB_COUNT_END = 0;

        /*
         * 当前下拉菜单勾选项
         */
        public static int CODE_OR_ENCODE_METHOD = 0;

        public static string SAVE_FILE_NAME = "outPrint.txt";

        public FormMain()
        {
            InitializeComponent();

            this.labelpictureUrl.Text = this.pictureBoxTabThree.Size.ToString();
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            checkHeadTailStatus();

            filesStringSubNowEachMyMethod(MYMETHOD_SUB_COUNT_END);
        }

        

        private void checkBoxHref_CheckedChanged(object sender, EventArgs e)
        {
            //href="//www.baidu.com/img/baidu.svg"
            if (this.checkBoxHref.Checked == true)
            {
                MYMETHOD_SUBSTRING_USE = 1;
                MYMETHOD_SUB_COUNT_END = 2;
                defaultMethodsChangeMode("HREF=\"", "\" ADD_", false, false, "checkBoxHref");
            }
        }

        private string regexTextStringEach(string strIn, string regStr)
        {
            //Try方法的运用，结果异常的返回，否则报错！
            Match mat = Regex.Match(strIn, regStr);
            string outStr = mat.Groups[0].Value;
            return outStr;
        }

        private void filesStringSubNowEachMyMethod(int type)
        {
            //读取文件地址不为空
            if (JudgementInFormMain.isARealFilesHere(this.textBoxUrl.Text))
            {

                ArrayList LineList = JudgementInFormMain.streamReaderOpenFiles(this.textBoxUrl.Text);
                ArrayList RegexList = new ArrayList();

                //正则表达式对字串进行过滤流程：
                for (int i = 0; i < LineList.Count; i++)
                {
                    string line = LineList[i].ToString();
                    if (line != null && !line.Equals(""))
                    {
                        //regexTextStringEach(line, this.textBoxRegex.Text);
                        if (this.checkBoxHTMethods.Checked == true)
                        {
                            //string strTest = "	<DT><A HREF=\"http://idb.rds.aliyun.com/\" ADD_DATE=\"1419570585\" LAST_MODIFIED=\"1419570585\">IDB CLOUD阿里云-数据库</A>";

                            if (isUsefulHeadTailString(this.textBoxHead.Text, this.textBoxTail.Text))
                            {
                                RegexList.Add(JudgementInFormMain.stringFindingForEach(line, this.textBoxHead.Text, this.textBoxTail.Text, type));
                            }
                            else
                            {
                                //起始字符串不合法
                                MessageBox.Show("请输入有效的【起始】和【结束】字符串", "提醒",
                                                     MessageBoxButtons.OK);
                                return;
                            }
                        }
                    }
                }

                /*
                //测试代码：
                string strTest = "	<DT><A HREF=\"http://idb.rds.aliyun.com/\" ADD_DATE=\"1419570585\" LAST_MODIFIED=\"1419570585\">IDB CLOUD阿里云-数据库</A>";
                string fileStr = regexTextStringEach(strTest, "^HREF(.*?)$ADD_DATE");
                fileStr = stringFindingForEach(strTest, "HREF=\"", "\" ADD_");
                streamReaderWriteFiles(fileStr);
                */

                //正常流程：
                string fileStr = string.Join("\n", (string[])RegexList.ToArray(typeof(string)));
                JudgementInFormMain.streamReaderWriteFiles(fileStr);

                //文件生成完毕：
                string locUrl = this.textBoxUrl.Text.Substring(0, this.textBoxUrl.Text.LastIndexOf('\\'));
                MessageBox.Show("\"铛铛铛铛\"处理结束,请查看\"" + locUrl + SAVE_FILE_NAME + "\"文件!", "提示",
                                                    MessageBoxButtons.OK);
            }
            else
            {
                //文件地址无效
                MessageBox.Show("未找到文件或文件地址错误！", "错误",
                                                    MessageBoxButtons.OK);
            }
        }

        

        private void whichMethodNowForRegexFiles()
        {
            switch (MYMETHOD_SUBSTRING_USE)
            {
                case 1:
                    this.checkBoxHref.Checked = true;
                    break;
                case 2:
                    this.checkBoxHttp.Checked = true;
                    break;
                case 3:
                    this.checkBoxSmallHref.Checked = true;
                    break;
                default:
                    break;//未知值默认为不操作！
            }

        }

        private void checkBoxIncludeHead_CheckedChanged(object sender, EventArgs e)
        {
            checkHeadTailStatus();
        }

        private void checkBoxIncludeTail_CheckedChanged(object sender, EventArgs e)
        {
            checkHeadTailStatus();
        }

        private void checkHeadTailStatus()
        {
            /*定义变量值:
            * 0 表示不包含头，且不包含尾
            * 1 表示不包含头，但包含尾
            * 2 表示包含头，但不包含尾
            * 3 表示包含头，且包含尾
            * 记忆逻辑参考二进数表示，如3二进制表示为11
            */
            if (this.checkBoxIncludeHead.Checked == true && this.checkBoxIncludeTail.Checked == true)
            {
                SUBSTRING_HEAR_INCLUDE = 3;
            }
            else if (this.checkBoxIncludeHead.Checked == false && this.checkBoxIncludeTail.Checked == true)
            {
                SUBSTRING_HEAR_INCLUDE = 1;
            }
            else if (this.checkBoxIncludeHead.Checked == true && this.checkBoxIncludeTail.Checked == false)
            {
                SUBSTRING_HEAR_INCLUDE = 2;
            }
            else
                SUBSTRING_HEAR_INCLUDE = 0;
        }

        private void defaultMethodsChangeMode(string head, string tail, Boolean headChecked, Boolean tailChecked, string ckName)
        {
            if ((head == null && head.Equals("")) || (tail == null && tail.Equals("")))
            {
                //参数不合法
                MessageBox.Show("请输入有效的【起始】和【结束】字符串", "提醒",
                                                    MessageBoxButtons.OK);
            }

            foreach (CheckBox c in groupBoxMyMethods.Controls)
            {
                if (c is System.Windows.Forms.CheckBox && c.Name != ckName)
                {
                    ((CheckBox)c).Checked = false;
                }
            }
            //whichMethodNowForRegexFiles();

            this.checkBoxHTMethods.Checked = true;
            this.textBoxHead.Text = head;
            this.textBoxTail.Text = tail;
            this.checkBoxIncludeHead.Checked = headChecked;
            this.checkBoxIncludeTail.Checked = tailChecked;
        }

        private void checkBoxHttp_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxHttp.Checked == true)
            {
                MYMETHOD_SUBSTRING_USE = 2;
                MYMETHOD_SUB_COUNT_END = 1;
                defaultMethodsChangeMode("http", "\" ADD_", true, false, "checkBoxHttp");
            }
        }

        private void checkBoxSmallHref_CheckedChanged(object sender, EventArgs e)
        {
            
            if (this.checkBoxSmallHref.Checked == true)
            {
                MYMETHOD_SUBSTRING_USE = 3;
                MYMETHOD_SUB_COUNT_END = 2;
                defaultMethodsChangeMode("href=\"", "\" ADD_", false, false, "checkBoxSmallHref");
            }
        }



        private Boolean isUsefulHeadTailString(string left, string right)
        {
            if ((left == null || left.Equals("")) || (right == null || right.Equals("")))
            {
                return false;
            }
            else
                return true;
        }

        /*
         * 多菜单同函数处理的实现，在DropDownItems下拉菜单的位置添加集体处理事件。
         * ****** this.MainHexadecimalItems.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] ******
         * this.MainHexadecimalItems.DropDownItemClicked += 
         *          new System.Windows.Forms.ToolStripItemClickedEventHandler(this.menuStripItemSelect);
         * 通过获取ToolStripItemClickedEventArgs e；e传参的内容即可包含所需要的信息。
         */
        private void menuStripItemSelect(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
        {
            //menuStripChg
            //if (MainHexadecimalItems.)
            JudgementInFormMain.menuStripChgSelectEvents(e.ClickedItem.Name);
            
            if (CODE_OR_ENCODE_METHOD != 0)
            {
                this.labelChangesMethodsDisplay.Text = "当前转换规则为：" + e.ClickedItem.OwnerItem.Text + " -> " + e.ClickedItem.Text;
            }

            //清空输入框？
            this.richTextBoxLeft.Text = "";
            this.richTextBoxRight.Text = "";
        }



        private void buttonEncodeNow_Click(object sender, EventArgs e)
        {
            string disText = CodingOrEncodingMethods.encodingAllTypeBy(CODE_OR_ENCODE_METHOD, this.richTextBoxLeft.Text);
            this.richTextBoxRight.Text = disText;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.richTextBoxRight.Text = "";
            this.richTextBoxLeft.Text = "";
        }

        private void left_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.richTextBoxRight.Text = this.richTextBoxLeft.Text;
            this.richTextBoxLeft.Text = "";
        }

        private void linkLabelRight_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.richTextBoxLeft.Text = this.richTextBoxRight.Text;
            this.richTextBoxRight.Text = "";
        }

        private void pictureBoxTabThree_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog(); //new一个方法

            //*.bmp;*.jpg;*.gif;*.png---------------限制图片类型,并筛选文件类型
            ofd.Filter = "(*.bmp;*.jpg;*.gif;*.png)|*.bmp;*.jpg;*.gif;*.png";
            
            ofd.ShowDialog(); //显示打开文件的窗口

            //显示文件引用位置、以Zoom缩略窗加载显示图片
            if (ofd.FileName != "" && ofd.FileName != null)
            {
                try
                {
                    this.labelpictureUrl.Text = ofd.FileName;
                    this.pictureBoxTabThree.Load(this.labelpictureUrl.Text);
                }
                catch
                {
                    //报错就报错，哥懒得理它。
                }
                
                //this.labelpictureTap.Text = ofd.FileName;
            }
        }

        private void pictureBoxTabThree_MouseEventsHover(object sender, EventArgs e)
        {
            this.labelpictureTap.Visible = true;
        }

        private void pictureBoxTabThree_MouseEventsLeave(object sender, EventArgs e)
        {
            this.labelpictureTap.Visible = false;
        }

        private void buttonPicToStream_Click(object sender, EventArgs e)
        {

        }

        private void buttonPicToBaseSF_Click(object sender, EventArgs e)
        {
            string outPutCode = "";
            try
            {
                outPutCode = CodingOrEncodingMethods.ImageFilesToBaseSixtyFourCode(this.pictureBoxTabThree.Image, this.pictureBoxTabThree.ImageLocation);
            }
            catch
            {
                //报错就报错，哥懒得理它。
            }

            this.richTextBoxTabThree.Text = outPutCode;
        }

        private void buttonTabThreeClear_Click(object sender, EventArgs e)
        {
            //相当于清除按钮
            this.richTextBoxTabThree.Text = "";
        }

        private void buttonBaseSFToPic_Click(object sender, EventArgs e)
        {
            //防止解析失败，一定要抛出异常
            try
            {
                this.pictureBoxTabThree.Image = CodingOrEncodingMethods.BaseSixtyFourCodeToImage(this.richTextBoxTabThree.Text);
            }
            catch
            {
                //报错就弹框提醒
                MessageBox.Show("解析成图片动作失败，请确认码源正确或可识别  -,-!!!", "错误提示",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonTabThreeSave_Click(object sender, EventArgs e)
        {
            System.Drawing.Bitmap img = null;
            SaveFileDialog sfd= new SaveFileDialog();
            sfd.InitialDirectory = "C:\\";
            sfd.Filter = "(*.bmp;*.jpg;*.gif;*.png)|*.bmp;*.jpg;*.gif;*.png";
            sfd.Title = "保存图像";
            sfd.ShowDialog();

            if (sfd.FileName != "")
            {
                string temp = sfd.FileName;
                img = new System.Drawing.Bitmap(this.pictureBoxTabThree.Image);
                img.Save("hehe.jpg");
                //待改进的方法。
            }

        }


    }
}
